home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1996 January: Mac OS SDK / Dev.CD Jan 96 SDK / Dev.CD Jan 96 SDK1.toast / Development Kits (Disc 1) / MacODBC / ODBC Tools / SampleSetup / Sources / DBMSSetup.c next >
Encoding:
C/C++ Source or Header  |  1995-03-18  |  8.7 KB  |  442 lines  |  [TEXT/MPS ]

  1. /*
  2.  *    DBMSsetup.c
  3.  *
  4.  *    Data source setup dialog, and its associated functions.
  5.  *
  6.  *    (c) Apple Computer, Inc 1993
  7.  */
  8.  
  9. #include <Controls.h>
  10. #include <Dialogs.h>
  11. #include <Events.h>
  12. #include <Lists.h>
  13. #include <Menus.h>
  14. #include <OSUtils.h>
  15. #include <Packages.h>
  16. #include <Quickdraw.h>
  17. #include <Resources.h>
  18. #include <String.h>
  19. #include <Strings.h>
  20. #include <TextEdit.h>
  21. #include <Types.h>
  22. #include <Windows.h>
  23. #include <TextUtils.h>
  24.  
  25. #include "DialogUtilities.h"
  26. #include "ODBCINST.H"
  27. #include "SQL.H"
  28. #include "SampleSetup.h"
  29.  
  30. #include "DBMSsetup.h"
  31.  
  32. /*
  33.  *    Defines
  34.  */
  35.  
  36. #define kDBMSsetupDlogID            134  
  37.  
  38. #define iOKButton                    1 
  39. #define iCancelButton                2 
  40. #define iDataSourceNameLabel        3 
  41. #define iDescriptionLabel            4 
  42. #define iOtherLabel                    5 
  43. #define iDataSourceName                6 
  44. #define iDescription                7 
  45. #define iOther                        8 
  46. #define iTranslationLabel            9 
  47. #define iTranslationName            10 
  48. #define iSelectButton                11 
  49. #define iLine1                        12 
  50. #define iLine2                        13 
  51. #define iTitle                        14 
  52. #define iTitleUnderline                15 
  53.  
  54. /*
  55.  *    Structures
  56.  */
  57.  
  58. typedef struct
  59. {
  60.     GrafPtr            savePort;
  61.     DialogPtr        dialog;
  62. }
  63. DoDBMSsetupRec;
  64.  
  65. /*
  66.  *    Defines
  67.  */
  68.  
  69. #define kStringTooLongAlertID        1000
  70.  
  71. #define    kMaxDescriptionLength        32
  72. #define    kMaxOtherLength                32
  73.  
  74. /*
  75.  *    Globals
  76.  */
  77.  
  78. static DoDBMSsetupRec    gDoDBMSsetupRec;
  79. static char                gTranslateDLL[256];
  80. static char                gTranslate[256];
  81. static char                gTranslateOption[256];
  82.  
  83. /*
  84.  *    Prototypes
  85.  */
  86.  
  87. static Boolean        Initialize                            (WindowPtr parentWindow, DataSourceInfo *info);
  88. static void            Cleanup                                ();
  89. static Boolean        DoOkButton                            (DataSourceInfo *info);
  90. static void            DoDataSourceName                    ();
  91. static void            DoDescription                        ();
  92. static void            DoOther                                ();
  93. static void            DoSelectButton                        ();
  94. static void            AdjustItems                            ();
  95. static Boolean        CheckNameFieldForValidity            ();
  96. static Boolean        CheckDescritptionFieldForValidity    ();
  97. static Boolean        CheckOtherFieldForValidity            ();
  98. static void            DoStringTooLongAlert                (short iField, short iFieldLabel, short maxChars);
  99. static void            NumToHexString                        (long num, char *ptr, long n);
  100. static void            HexStringToNum                        (char *ptr, long *num);
  101.  
  102. /*
  103.  *    Public functions
  104.  */
  105.  
  106. Boolean
  107. DoDBMSsetup(WindowPtr parentWindow, DataSourceInfo *info)
  108. {
  109.     /*
  110.      *    Display the data source setup dialog, centered over parentWindow, and
  111.      *    handle events until the user dismisses the dialog.
  112.      */
  113.     
  114.     Boolean        result;
  115.     Boolean        done = false;
  116.     short        item;
  117.     
  118.     UPPInit( uppModalFilterProcInfo, StandardFilter )
  119.     
  120.     if (!Initialize(parentWindow, info)) return false;
  121.     
  122.     while (!done)
  123.     {
  124.         ModalDialog( UPP(StandardFilter), &item);
  125.         switch (item)
  126.         {
  127.             case iOKButton:
  128.                 done = result = DoOkButton(info);
  129.                 break;
  130.             
  131.             case iCancelButton:
  132.                 result = false;
  133.                 done = true;
  134.                 break;
  135.             
  136.             case iDataSourceName:
  137.                 DoDataSourceName();
  138.                 break;
  139.             
  140.             case iDescription:
  141.                 DoDescription();
  142.                 break;
  143.             
  144.             case iOther:
  145.                 DoOther();
  146.                 break;
  147.             
  148.             case iSelectButton:
  149.                 DoSelectButton();
  150.                 break;
  151.         }
  152.     }
  153.     
  154.     Cleanup();
  155.     
  156.     return result;
  157. }
  158.  
  159. /*
  160.  *    Private functions
  161.  */
  162.  
  163. UPPInitStatic(uppUserItemProcInfo,DrawItemLineGray)
  164. UPPInitStatic(uppUserItemProcInfo,DrawItemDoubleLine)
  165.  
  166. static Boolean
  167. Initialize(WindowPtr parentWindow, DataSourceInfo *info)
  168. {
  169.     /*
  170.      *    Create the dialog and initialize its items - return false if anything fails.
  171.      */
  172.     
  173.     DialogPtr    dialog = NULL;
  174.     GrafPtr        savePort = NULL;
  175.     
  176.     GetPort(&savePort);
  177.     InitCursor();
  178.     dialog = GetNewDialogOverWindow(kDBMSsetupDlogID, parentWindow);
  179.     if (dialog == NULL) goto bail;
  180.     SetPort(dialog);
  181.     
  182.     SetDUserItem(dialog, iLine1,            (UniversalProcPtr)UPP(DrawItemLineGray) );
  183.     SetDUserItem(dialog, iLine2,            (UniversalProcPtr)UPP(DrawItemLineGray) );
  184.     SetDUserItem(dialog, iTitleUnderline,    (UniversalProcPtr)UPP(DrawItemDoubleLine) );
  185.     
  186.     c2pstr(info->name);
  187.     SetDText(dialog, iDataSourceName, info->name);
  188.     p2cstr(info->name);
  189.     
  190.     c2pstr(info->description);
  191.     SetDText(dialog, iDescription, info->description);
  192.     p2cstr(info->description);
  193.     
  194.     c2pstr(info->other);
  195.     SetDText(dialog, iOther, info->other);
  196.     p2cstr(info->other);
  197.     
  198.     strcpy(gTranslateDLL, info->translateDLL);
  199.     strcpy(gTranslate, info->translate);
  200.     strcpy(gTranslateOption, info->translateOption);
  201.     
  202.     gDoDBMSsetupRec.savePort = savePort;
  203.     gDoDBMSsetupRec.dialog = dialog;
  204.     
  205.     AdjustItems();
  206.     ShowWindow(dialog);
  207.     
  208.     return true;
  209.     
  210. bail:
  211.     
  212.     if (dialog != NULL) DisposDialog(dialog);
  213.     SetPort(savePort);
  214.     
  215.     return false;
  216. }
  217.  
  218. static void
  219. Cleanup()
  220. {
  221.     /*
  222.      * Dispose of the dialog and its assundry allocations
  223.      */
  224.     
  225.     DisposDialog(gDoDBMSsetupRec.dialog);
  226.     SetPort(gDoDBMSsetupRec.savePort);
  227. }
  228.  
  229. static Boolean
  230. DoOkButton(DataSourceInfo *info)
  231. {
  232.     /*
  233.      *    Copy the dialog settings to the DataSourceInfo structure
  234.      */
  235.     
  236.     if (!CheckNameFieldForValidity())
  237.         return false;
  238.     if (!CheckDescritptionFieldForValidity())
  239.         return false;
  240.     if (!CheckOtherFieldForValidity())
  241.         return false;
  242.     
  243.     GetDText(gDoDBMSsetupRec.dialog, iDataSourceName, info->name);
  244.     p2cstr(info->name);
  245.     GetDText(gDoDBMSsetupRec.dialog, iDescription, info->description);
  246.     p2cstr(info->description);
  247.     GetDText(gDoDBMSsetupRec.dialog, iOther, info->other);
  248.     p2cstr(info->other);
  249.     strcpy(info->translate, gTranslate);
  250.     strcpy(info->translateDLL, gTranslateDLL);
  251.     strcpy(info->translateOption, gTranslateOption);
  252.     
  253.     return true;
  254. }
  255.  
  256. static void
  257. DoDataSourceName()
  258. {
  259.     /*
  260.      *    Handle changes to the "name" edit field
  261.      */
  262.     
  263.     AdjustItems();
  264. }
  265.  
  266. static void
  267. DoDescription()
  268. {
  269.     /*
  270.      *    Handle changes to the "description" edit field
  271.      */
  272.     
  273.     ;
  274. }
  275.  
  276. static void
  277. DoOther()
  278. {
  279.     /*
  280.      *    Handle changes to the "other" edit field
  281.      */
  282.     
  283.     ;
  284. }
  285.  
  286. static void
  287. DoSelectButton()
  288. {
  289.     /*
  290.      *    Call the ODBC Configuration Manager to get the translator name and option.
  291.      */
  292.     
  293.     char    translate[256], translateDLL[256];
  294.     UINT    translateSize, translateDLLSize;
  295.     DWORD    option;
  296.     long    optionLong;
  297.     Boolean    success;
  298.     
  299.     strcpy(translate, gTranslateDLL);
  300.     strcpy(translate, gTranslate);
  301.     HexStringToNum(gTranslateOption, &optionLong);
  302.     option = (DWORD) optionLong;
  303.     
  304.     success = (Boolean) SQLGetTranslator(gDoDBMSsetupRec.dialog, translate, 256, &translateSize,
  305.                                          translateDLL, 256, &translateDLLSize, &option);
  306.     
  307.     if (success)
  308.     {
  309.         strcpy(gTranslateDLL, translateDLL);
  310.         strcpy(gTranslate, translate);
  311.         NumToHexString(option, gTranslateOption, 8);
  312.         
  313.         AdjustItems();
  314.     }
  315. }
  316.  
  317. static void
  318. AdjustItems()
  319. {
  320.     /*
  321.      *    Enable/disable dialog items as appropriate
  322.      */
  323.     
  324.     char    name[256];
  325.     
  326.     GetDText(gDoDBMSsetupRec.dialog, iDataSourceName, name);
  327.     EnableDItem(gDoDBMSsetupRec.dialog, iOKButton, (name[0] > 0));
  328.     OutlineDItem(gDoDBMSsetupRec.dialog, iOKButton);
  329.     
  330.     c2pstr(gTranslate);
  331.     SetDText(gDoDBMSsetupRec.dialog, iTranslationName, gTranslate);
  332.     p2cstr(gTranslate);
  333. }
  334.  
  335. static Boolean
  336. CheckNameFieldForValidity()
  337. {
  338.     /*
  339.      *    Make sure the "Data Source Name" edit text field isn't too long.
  340.      */
  341.     
  342.     char    str[256];
  343.     
  344.     GetDText(gDoDBMSsetupRec.dialog, iDataSourceName, str);
  345.     if (str[0] > SQL_MAX_DSN_LENGTH)
  346.     {
  347.         DoStringTooLongAlert(iDataSourceName, iDataSourceNameLabel, SQL_MAX_DSN_LENGTH);
  348.         return false;
  349.     }
  350.     
  351.     return true;
  352. }
  353.  
  354. static Boolean
  355. CheckDescritptionFieldForValidity()
  356. {
  357.     /*
  358.      *    Make sure the "Description" edit text field isn't too long.
  359.      */
  360.     
  361.     char    str[256];
  362.     
  363.     GetDText(gDoDBMSsetupRec.dialog, iDescription, str);
  364.     if (str[0] > kMaxDescriptionLength)
  365.     {
  366.         DoStringTooLongAlert(iDescription, iDescriptionLabel, kMaxDescriptionLength);
  367.         return false;
  368.     }
  369.     
  370.     return true;
  371. }
  372.  
  373. static Boolean
  374. CheckOtherFieldForValidity()
  375. {
  376.     /*
  377.      *    Make sure the "Other" edit text field isn't too long.
  378.      */
  379.     
  380.     char    str[256];
  381.     
  382.     GetDText(gDoDBMSsetupRec.dialog, iOther, str);
  383.     if (str[0] > kMaxOtherLength)
  384.     {
  385.         DoStringTooLongAlert(iOther, iOtherLabel, kMaxOtherLength);
  386.         return false;
  387.     }
  388.     
  389.     return true;
  390. }
  391.  
  392. static void
  393. DoStringTooLongAlert(short iField, short iFieldLabel, short maxChars)
  394. {
  395.     /*
  396.      *    Put up an alert that says something like ""Blather" cannot be longer
  397.      *    than 51 characters".  Then when the user clicks OK hilite that field.
  398.      */
  399.     
  400.     char    label[256], num[256];
  401.     
  402.     GetDText(gDoDBMSsetupRec.dialog, iFieldLabel, label);
  403.     if (label[*label] == ':')
  404.         *label -= 1;
  405.     NumToString(maxChars, num);
  406.     ParamText(label, num, "", "");
  407.     AlertOverWindow(kStringTooLongAlertID, NULL, gDoDBMSsetupRec.dialog);
  408.     SelIText(gDoDBMSsetupRec.dialog, iField, 0, 32767);
  409. }
  410.  
  411. static void
  412. NumToHexString(long num, char *str, long n)
  413. {
  414.     char *digits = "0123456789ABCDEF";
  415.     
  416.     while (n-- > 0)
  417.         *str++ = digits[(num >> (4*n)) & 0x000F];
  418.     
  419.     *str = '\0';
  420. }
  421.  
  422. static void
  423. HexStringToNum(char *str, long *num)
  424. {
  425.     char *p = str, ch;
  426.     long v = 0;
  427.     
  428.     while (*p == ' ' || *p == '\t') p++;
  429.     
  430.     for (;;)
  431.     {
  432.         if (! (ch = *p++)) { p--; break; }
  433.         
  434.             if (ch >= '0' && ch <= '9') { v <<= 4; v += ch - '0'; }
  435.         else if (ch >= 'a' && ch <= 'f') { v <<= 4; v += ch - 'a' + 10; }
  436.         else if (ch >= 'A' && ch <= 'F') { v <<= 4; v += ch - 'A' + 10; }
  437.         else break;
  438.     }
  439.     
  440.     *num = v;
  441. }
  442.